﻿using System;
using System.Collections.Generic;
using System.Text;

namespace store.cyt369.com.models.WebRequest
{
    public static class URLHelper
    {
        public static string GetFileName(string url)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(url))
            {
                string[] array = url.Split(new char[]
                {
                    '?'
                });
                if (array != null && array.Length > 0)
                {
                    int i = array[0].Length - 1;
                    while (i >= 0)
                    {
                        if (array[0][i] == '/' || array[0][i] == ':')
                        {
                            if (i != array[0].Length - 1)
                            {
                                i++;
                                result = array[0].Substring(i, array[0].Length - i);
                                break;
                            }
                            break;
                        }
                        else
                        {
                            i--;
                        }
                    }
                    if (i < 0)
                    {
                        result = array[0];
                    }
                }
            }
            return result;
        }

        public static string RemoveFile(string url)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(url))
            {
                for (int i = url.Length - 1; i >= 0; i--)
                {
                    if (url[i] == '/' || url[i] == ':')
                    {
                        result = url.Substring(0, i);
                        break;
                    }
                }
            }
            return result;
        }

        public static string CombinURL(string str1, string str2)
        {
            string text = string.Empty;
            if (!string.IsNullOrEmpty(str1) && !string.IsNullOrEmpty(str2))
            {
                text = str1;
                if (str1[str1.Length - 1] != '/')
                {
                    text += '/';
                }
                for (int i = 0; i < str2.Length; i++)
                {
                    if (str2[i] != '.' && str2[i] != '/')
                    {
                        text += str2.Substring(i, str2.Length - i);
                        break;
                    }
                }
            }
            return text;
        }

        public static string AddParameter(string url, string key, object value)
        {
            if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(key))
            {
                if (url.IndexOf('?') > 0)
                {
                    url += string.Format("&{0}={1}", key, (value != null) ? value.ToString() : string.Empty);
                }
                else
                {
                    url += string.Format("?{0}={1}", key, (value != null) ? value.ToString() : string.Empty);
                }
            }
            return url;
        }

        public static string UrlEncode(string url, System.Text.Encoding encoding = null)
        {
            if (encoding == null)
            {
                return System.Web.HttpUtility.UrlEncode(url);
            }
            else
            {
                return System.Web.HttpUtility.UrlEncode(url, encoding);
            }
        }

        public static List<int> ArgumentToInts(string args, params char[] separator)
        {
            var list = new List<int>();
            if (!string.IsNullOrEmpty(args))
            {
                var sps = args.Split(separator,  StringSplitOptions.RemoveEmptyEntries);
                foreach (var s in sps)
                {
                    var i = 0;
                    if (int.TryParse(s,  out i))
                    {
                        list.Add(i);
                    }
                }
            }

            return list;
        }
    }

}
